#pragma hdrstop

#include "Globals.h"

#pragma package(smart_init)
#pragma resource "*.dfm"
TMiniForm *MiniForm;
//---------------------------------------------------------------------------
__fastcall TMiniForm::TMiniForm(TComponent* Owner)
    : TForm(Owner)
{
}

//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::FormPaint(TObject *Sender)
{
  char buf[64];

  TEMP->Width  = 16;
  TEMP->Height = 16;
  // colors
  for ( int i = 0; i < 16; i++ )
    memcpyw((WORD *)TEMP->ScanLine[ i ], &DOS565[ i << 4 ], 16, 0 );
  //
  ColPic->Canvas->StretchDraw( ColPic->ClientRect, TEMP );

  TEMP->Width  = 8;
  TEMP->Height = 8;
  // char
  for ( int i = 0; i < 8; i++ )
    memcpyw((WORD *)TEMP->ScanLine[ i ], &cur_tile[ i << 3 ], 8, 0 );
  //
  ChrPic->Canvas->StretchDraw( ChrPic->ClientRect, TEMP );
  //

  sprintf(buf,"Tile Editor - [L:%04X, R:%04X]", rat.y,rat.x);
  MiniForm->Caption = AnsiString( buf );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::FlipYClick(TObject *Sender)
{
  picrev(cur_tile, 8, 8, 2);
  FormPaint( NULL );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::FlipXClick(TObject *Sender)
{
  picrev(cur_tile, 8, 8, 1);
  FormPaint( NULL );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::Button1Click(TObject *Sender)
{
  picrev(cur_tile, 8, 8, 4);
  FormPaint( NULL );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::ChrPicMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
  TRect r;

  if ( (X < 0 || Y < 0) || (X >= ChrPic->Width || Y >= ChrPic->Height) )
     return;
  //
  X = X >> 3;
  Y = Y >> 3;

  // Make sure we don't want the color from the CHAR
  if ( KEY_DOWN(VK_CONTROL) )
  {
    // Left color
    if ( Button == mbLeft )
    {
      rat.y = cur_tile[(Y << 3) + X];
      r = Rect(0, 0, 30, RCol->Height);
      RCol->Canvas->Brush->Color = (TColor)_16To24( rat.y );
      RCol->Canvas->FillRect( r );
    }
    else
    // Right color
    {
      rat.x = cur_tile[(Y << 3) + X];
      //
      r = Rect(31, 0, 60, RCol->Height);
      RCol->Canvas->Brush->Color = (TColor)_16To24( rat.x );
      RCol->Canvas->FillRect( r );
    }
  }
  else
  // Standard Mouse operations
  {
    // Store color
    if ( Button == mbLeft )
      cur_tile[(Y << 3) + X] = rat.y;
    else
      cur_tile[(Y << 3) + X] = rat.x;
  }// END IF
  //
  FormPaint( NULL );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::ColPicMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
  TRect r;

  if ( (X < 0 || Y < 0) || (X >= ColPic->Width || Y >= ColPic->Height) )
     return;
  //
  X = X >> 2;
  Y = Y >> 2;

  // Store color
  if ( Button == mbLeft ){
    rat.y = DOS565[ (Y << 4) + X];
    //
    r = Rect(0, 0, 30, RCol->Height);
    RCol->Canvas->Brush->Color = (TColor)_16To24( rat.y );
    RCol->Canvas->FillRect( r );
  }else{
    rat.x = DOS565[ (Y << 4) + X];
    //
    r = Rect(31, 0, 60, RCol->Height);
    RCol->Canvas->Brush->Color = (TColor)_16To24( rat.x );
    RCol->Canvas->FillRect( r );
  }
  //
  FormPaint( NULL );
}



//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::ColPicMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
  TRect r;
  WORD cCol;

  if ( (X < 0 || Y < 0) || (X >= ColPic->Width || Y >= ColPic->Height) )
     return;

  if ( KEY_DOWN(VK_LBUTTON) ) ColPicMouseDown(Sender, mbLeft , Shift, X, Y);
  if ( KEY_DOWN(VK_RBUTTON) ) ColPicMouseDown(Sender, mbRight, Shift, X, Y);
  //
  X            = X >> 2;
  Y            = Y >> 2;
  // Clear old data
  if ( col.y != -1 && col.x != -1 )
  {
    r = Rect(col.x, col.y, col.x+4, col.y+4);
    ColPic->Canvas->Brush->Color = (TColor)_16To24( cCol = DOS565[ ((col.y >> 2) << 4) + (col.x >> 2) ] );
    ColPic->Canvas->FillRect( r );
  }

  // save the last position
  col.y = r.Top   = Y << 2;
  col.x = r.Left  = X << 2;
  r.Bottom        = (Y << 2) + 4;
  r.Right         = (X << 2) + 4;

  ColPic->Canvas->Brush->Color = ( (cCol >= 0xFFF0) ? (TColor)_16To24( 0x0FFFF ^ cCol ) : clWhite );
  ColPic->Canvas->FrameRect( r );
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::ChrPicMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
  TRect r;
  WORD cCol;

  if ( (X < 0 || Y < 0) || (X >= ChrPic->Width || Y >= ChrPic->Height) )
     return;

  if ( KEY_DOWN(VK_LBUTTON) ) ChrPicMouseDown(Sender, mbLeft , Shift, X, Y);
  if ( KEY_DOWN(VK_RBUTTON) ) ChrPicMouseDown(Sender, mbRight, Shift, X, Y);
  //
  X            = X >> 3;
  Y            = Y >> 3;
  // Clear old data
  if ( chr.y != -1 && chr.x != -1 )
  {
    r = Rect(chr.x, chr.y, chr.x+8, chr.y+8);
    ChrPic->Canvas->Brush->Color = (TColor)_16To24( cCol = cur_tile[ ((chr.y >> 3) << 3) + (chr.x >> 3) ] );
    ChrPic->Canvas->FillRect( r );
  }

  // save the last position
  chr.y = r.Top   = Y << 3;
  chr.x = r.Left  = X << 3;
  r.Bottom        = (Y << 3) + 8;
  r.Right         = (X << 3) + 8;

  ChrPic->Canvas->Brush->Color = (TColor)_16To24( 0x0FFFF ^ cCol );
  ChrPic->Canvas->FrameRect( r );
  //
  r.Top    = (Y << 3) + 1;
  r.Left   = (X << 3) + 1;
  r.Bottom = (Y << 3) + 7;
  r.Right  = (X << 3) + 7;
  ChrPic->Canvas->FrameRect( r );  
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::FormCreate(TObject *Sender)
{
  col.y = col.x = -1;
  chr.y = chr.x = -1;
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::ShfLeftClick(TObject *Sender)
{
  int i, x;

  for ( i = 0; i < 8; i++ )
  {
    for ( x = 0; x < 7; x++ )
      cur_tile[(i << 3) + x] = cur_tile[(i << 3) + (x + 1)];
    cur_tile[(i << 3) + x] = 0;
  }
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::ShfRightClick(TObject *Sender)
{
  int i, x;

  for ( i = 0; i < 8; i++ )
  {
    for ( x = 7; x > 0; x-- )
      cur_tile[(i << 3) + x] = cur_tile[(i << 3) + (x - 1)];
    cur_tile[(i << 3)] = 0;
  }
}


//////////////////////////////////
//                              //
//////////////////////////////////
void __fastcall TMiniForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
  if ( Key == VK_UP   ) rat.y--;
  if ( Key == VK_DOWN ) rat.y++;
  if ( Key == VK_LEFT ) rat.x--;
  if ( Key == VK_RIGHT) rat.x++;
  //
  rat.y &= 0xFFFF;
  rat.x &= 0xFFFF;
  MiniForm->FormPaint( NULL );
}


